//
//  SLToggleChooseView.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/22.
//

#import "SLToggleChooseView.h"
#import <YYKit.h>
#import <Masonry.h>

@interface SLToggleChooseItem : UIView
@property (nonatomic, strong) UIView *container;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, assign) BOOL isSelected;
@end

@implementation SLToggleChooseItem

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self addSubview:self.container];
        [self.container mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self);
        }];
        [self addSubview:self.icon];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.container);
            make.top.bottom.equalTo(self.container);
            make.width.height.mas_equalTo(23);
        }];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.icon.mas_right).offset(13);
            make.right.equalTo(self.container);
            make.top.bottom.equalTo(self.container);
        }];
        
    }
    return self;
}

- (void)setIsSelected:(BOOL)isSelected {
    _isSelected = isSelected;
    if (isSelected) {
        self.icon.image = [UIImage imageNamed:@"icon_sex_select"];
        self.titleLabel.textColor = UIColorHex(#333333);
    } else {
        self.icon.image = [UIImage imageNamed:@"icon_sex_normal"];
        self.titleLabel.textColor = UIColorHex(#666666);
    }
}

- (UIView *)container {
    if (!_container) {
        _container = [UIView new];
    }
    return _container;
}
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [UIImageView new];
        _icon.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _icon;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"Male";
        _titleLabel.font = aspectDefaultFont(16);
    }
    return _titleLabel;
}

@end

@interface SLToggleChooseView ()
@property (nonatomic, strong) SLToggleChooseItem *leftItem;
@property (nonatomic, strong) SLToggleChooseItem *rightItem;
@property (nonatomic, copy  ) NSArray *data;
@property (nonatomic, assign) NSInteger selectIndex;
@end

@implementation SLToggleChooseView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        [self.customView addSubview:self.leftItem];
        [self.customView addSubview:self.rightItem];
        
        NSArray *arr = @[self.leftItem, self.rightItem];
        [arr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:20 leadSpacing:77 tailSpacing:77];
        [arr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(self.customView);
        }];
        
        self.selectIndex = 0;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    [self.customView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(86);
    }];
}

- (void)setData:(NSArray *)data {
    _data = data;
    if (data.count != 2) {
        return;
    }
    if (self.titleCallback) {
        self.leftItem.titleLabel.text = self.titleCallback(0);
        self.rightItem.titleLabel.text = self.titleCallback(1);
    }
}
- (void)setSelectIndex:(NSInteger)selectIndex {
    _selectIndex = selectIndex;
    if (selectIndex == 0) {
        self.leftItem.isSelected = YES;
        self.rightItem.isSelected = NO;
    } else {
        self.leftItem.isSelected = NO;
        self.rightItem.isSelected = YES;
    }
}

#pragma mark - 点击事件
- (void)confirmButtonAction:(UIButton *)button {
    [super confirmButtonAction:button];
    !self.confirmButtonCallback ?: self.confirmButtonCallback(@(self.selectIndex));
}

- (SLToggleChooseItem *)leftItem {
    if (!_leftItem) {
        _leftItem = [SLToggleChooseItem new];
        _leftItem.titleLabel.text = @"Male";
        @weakify(self);
        [_leftItem jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            self.selectIndex = 0;
        }];
    }
    return _leftItem;
}
- (SLToggleChooseItem *)rightItem {
    if (!_rightItem) {
        _rightItem = [SLToggleChooseItem new];
        _leftItem.titleLabel.text = @"Female";
        @weakify(self);
        [_rightItem jk_addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            @strongify(self);
            self.selectIndex = 1;
        }];
    }
    return _rightItem;
}

@end





@implementation SLToggleChooseView (Fast)

+ (instancetype)showToggleChooseWithData:(NSArray *)arr titleCallback:(NSString *(^)(NSInteger index))titleCallback confirmCallback:(void(^)(NSInteger index))block {
    SLToggleChooseView *alert = [SLToggleChooseView new];
    alert.titleLabel.text = @"Select Gender";
    alert.titleCallback = titleCallback;
    alert.confirmButtonCallback = ^(id obj) {
        !block ?: block([obj sl_intValue]);
    };
    alert.data = arr;
    [UIApplication.sharedApplication.windows.firstObject addSubview:alert];
    [alert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(alert.superview);
    }];
    
    return alert;
}

@end
